﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASS.BUS.DTO;
using ASS.BUS.Services;


namespace ASS
{
    public partial class FrmCreateAppointment : Form
    {
        public FrmCreateAppointment()
        {
            InitializeComponent();
        }

        public FrmCreateAppointment(FrmMainClinicStaff frmMain)
        {
            _frmMain = frmMain;
            InitializeComponent();
        }
        public FrmCreateAppointment(FrmMainClinicStaff frmMain, BenhNhan benhnhan)
        {
            _benhNhan = benhnhan;
            _frmMain = frmMain;
            InitializeComponent();
        }
        private BenhNhan _benhNhan = new BenhNhan();
        private BenhNhanServices _benhNhanServices = new BenhNhanServices();
        private NhanVien _nhanVien = new NhanVien();
        private NhanVienServices _nhanVienServices = new NhanVienServices();
        private FrmMainClinicStaff _frmMain;

        private void btnSave_Click(object sender, EventArgs e)
        {
            LichHen lichHen = new LichHen();
            LichHenServices lichHenServices = new LichHenServices();

            if (_benhNhan != null)
            {
                lichHen.NgayHen = dtpDateAppointment.Value.ToString();              
                lichHen.NoiDung = rtfContent.Text;
                //lichHen.Nhan = _benhNhan;
                lichHen.BenhNhan = _benhNhan.Ten;
               
                if (_nhanVien != null)
                {
                    //lichHen.NhanVien = _nhanVien;
                    lichHen.BacSi = _nhanVien.Ten;
                }

                bool addSuccess = lichHenServices.Add(lichHen);
                if (addSuccess)
                {
                    MessageBox.Show(@"Thêm thành công", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    _frmMain.dvgLichhen.DataSource = lichHenServices.GetLichHens();
                   // this.Close();
                }
            }
        }

        private void FrmCreateAppointment_Load(object sender, EventArgs e)
        {
           
            var benhNhans = _benhNhanServices.GetBenhNhans();
            var benhNhanList = benhNhans.Select(benhNhan => benhNhan.MaBn).ToList();
            cboMaBenhNhan.DataSource = benhNhanList;

            var nhanViens = _nhanVienServices.GetBacSis();
            var nhanVienList = nhanViens.Select(nv => nv.MaNv).ToList();
            cboDoctor.DataSource = nhanVienList;
        }

        private void cboDoctor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboDoctor.SelectedIndex == -1) return;
            var id = Int32.Parse(cboDoctor.SelectedValue.ToString());
            _nhanVien = _nhanVienServices.GetNhanVien(id);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cboMaBenhNhan_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cboMaBenhNhan.SelectedIndex == -1) return;
            var id = Int32.Parse(cboMaBenhNhan.SelectedValue.ToString());
            _benhNhan = _benhNhanServices.GetBenhNhan(id);
            if (_benhNhan != null)
            {
                txtNamePatient.Text = _benhNhan.Ten;

            }
        }
    }
}
